package com.ti.demo.configuration;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ti.demo.entity.CityRequest;
import com.ti.demo.entity.WeatherInfo;
import com.ti.demo.entity.WeatherRequest;
import com.ti.demo.entity.WeatherResponse;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.web.client.RestClient;

import java.util.function.Function;

/**
 * @description: com.ti.demo.configuration
 * @Author Triagen
 * @Date 2025/10/9 11:01
 */
@Configuration
public class WeatherConfiguration {

    @Bean
    public RestClient getRestClient(ObjectProvider<RestClient.Builder> restClientBuilderProvider) {
        return restClientBuilderProvider.getIfAvailable(RestClient::builder).baseUrl("https://weather.cma.cn/api").build();
    }

    // 使用 Function Bean，此代码注释
//    @Bean("getWeatherInfoToolCallback")
//    public ToolCallback getWeatherInfoToolCallback(RestClient restClient) {
//        return FunctionToolCallback.builder("getWeatherInfoToolCallback", (Function<WeatherRequest, WeatherInfo>) weatherRequest -> {
//                    WeatherResponse weatherResponse = restClient.get().uri("/now/" + weatherRequest.getCityId()).retrieve().body(WeatherResponse.class);
//                    return weatherResponse.getData();
//                })
//                .inputType(WeatherRequest.class)
//                .description("Get weather info by cityId")
//                .build();
//    }

    @Description("get weather info by cityId")   // 如果去掉此注解，则默认拆分beanName，即描述信息为 get weather info
    @Bean
    public Function<WeatherRequest, WeatherInfo> getWeatherInfo(RestClient restClient){
        return weatherRequest -> {
            WeatherResponse weatherResponse = restClient.get().uri("/now/" + weatherRequest.getCityId()).retrieve().body(WeatherResponse.class);
            return weatherResponse.getData();
        };
    }


    @Bean
    public static ToolCallback getCityId(RestClient restClient) {
        return FunctionToolCallback.builder("getCityId", (Function<CityRequest, String>) cityRequest -> {
                    String body = restClient.get().uri("/map/weather/1").retrieve().body(String.class);
                    JSONObject jsonObject = JSON.parseObject(body);
                    return jsonObject.getJSONObject("data").getJSONArray("city").stream()
                            .filter(o -> cityRequest.getCityName().equals(((JSONArray) o).getString(1)))
                            .findFirst()
                            .map(o -> ((JSONArray) o).getString(0))
                            .get();
                })
                .inputType(CityRequest.class)
                .description("To get weather info, we need to use cityId, this tool is used to get cityId from cityName")
                // 输入inputType 后会自动解析，这里注释掉，仅供参考
//                .inputSchema("""
//                        {
//                          "$schema" : "https://json-schema.org/draft/2020-12/schema",
//                          "type" : "object",
//                          "properties" : {
//                            "cityName" : {
//                              "type" : "string",
//                              "description" : "cityName, the language should be Chinese"
//                            }
//                          },
//                          "required" : [ "cityName" ],
//                          "additionalProperties" : false
//                        }
//                        """)
                .build();
    }


}
